package com.tanhua.server.controller;

import com.tanhua.common.pojo.Answers;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.QuestionnaireVo;
import com.tanhua.server.vo.TestResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 测灵魂
 */

@RestController
@RequestMapping("testSoul")
@Slf4j
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 问卷列表
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> queryTestSoul() {

        try {
            List<QuestionnaireVo> testSoulVo = this.testSoulService.queryTestSoul();
            //判断返回的结果是不是空的
            if (testSoulVo != null) {
                return ResponseEntity.ok(testSoulVo);
            }
        } catch (Exception e) {
            log.error("查询问卷列表失败", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /**
     * 提交问卷
     *
     * @param map
     */
    @PostMapping
    public ResponseEntity<String> saveTestSoul(@RequestBody Map<String, List<Answers>> map) {

        try {
            List<Answers> answersList = map.get("answers");

            String s = testSoulService.saveAnswers(answersList);

            if (s != null) {
                return ResponseEntity.ok(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping("report/{id}")
    public ResponseEntity<TestResultVo> testSoulResult(@PathVariable("id") Long id) {

        try {
            TestResultVo testResultVo = this.testSoulService.testResult(id);
            return ResponseEntity.ok(testResultVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
